<template>
  <div class="container">
    <!-- 操作 -->
    <div class="dis-o-between operate-line">
      <div>
        <el-button icon="el-icon-plus" plain type="primary" @click="addItem" size="small">新增</el-button>
        <el-button icon="el-icon-sort" plain type="info" @click="expandAllClick()" size="small">展开/折叠</el-button>
      </div>
      <RightToolbar :search="false" @queryTable="getList()"></RightToolbar>
    </div>
    <el-table v-loading="loading" :data="tableData" :default-expand-all="expandAll" row-key="id"
      :tree-props="{ children: 'list' }" v-if="refreshTable">
      <el-table-column prop="facilityTypeName" label="设施分类名称"></el-table-column>
      <el-table-column prop="remark" label="分类备注" align="center"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template #default='scope'>
          <el-button class="btn" type="text" @click="editRow(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.parentFacilityTypeUuid == null || scope.row.parentFacilityTypeUuid == ''"
            class="btn" type="text" @click="addChild(scope.row)">新增子类</el-button>
          <el-button class="btn" type="text" @click="delRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-dialogDrag :title="title" :visible.sync="addVisible" :destroy-on-close='true' width="30%">
      <el-form :model="ruleForm" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-form-item label="设施分类名称" :rules="{ required: true, message: '设施分类名称不能为空', trigger: 'blur' }"
          prop="facilityTypeName">
          <el-input v-model="ruleForm.facilityTypeName" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="分类编号" :rules="{ required: true, message: '分类编号不能为空', trigger: 'blur' }"
          prop="facilityTypeCode">
          <el-input type="number" :disabled="editShow" v-model="ruleForm.facilityTypeCode" placeholder="请输入"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="分类备注">
          <el-input v-model="ruleForm.remark" placeholder="请输入" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'type',
  data() {
    return {
      loading: false,
      expandAll: false,
      refreshTable: true,
      tableData: [],
      addVisible: false,
      ruleForm: {
        facilityTypeName: '',
        id: '',
        parentFacilityTypeUuid: '',
        remark: '',
        facilityTypeCode: '',
      },

      title: '',
      editShow: false,
    };
  },
  props: {},

  components: {},

  created() { },

  mounted() { this.getList() },

  beforeDestroy() { },

  methods: {
    expandAllClick() {
      this.refreshTable = false;
      // console.log(this.expandAll);
      this.expandAll = !this.expandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 初始请求
    getList() {
      this.loading = true
      this.tableData = []
      this.sendPost('/facilityType/getFacilityTypeByObj', {}).then(res => {
        this.tableData = res.data.list;
        this.loading = false
      })
    },
    // 新增设施分类
    addItem() {
      this.ruleForm = {};
      this.editShow = false;
      this.addVisible = true;
    },
    // 添加子类
    addChild(row) {
      this.ruleForm.parentFacilityTypeUuid = row.facilityTypeUuid;
      this.addVisible = true;
      this.editShow = false;
    },
    // 编辑
    editRow(row) {
      console.log('编辑', row);
      this.ruleForm = { facilityTypeName: row.facilityTypeName, id: row.id, parentFacilityTypeUuid: row.parentFacilityTypeUuid, remark: row.remark, facilityTypeCode: row.facilityTypeCode, };
      this.addVisible = true;
      this.editShow = true;
    },
    // 新增设施确定
    addSubmit(data) {
      this.$refs[data].validate((valid) => {
        if (valid) {
          this.sendPost('/facilityType/insertOrUpdateFacilityType', this.ruleForm).then(res => {
            if (res.errcode == 0) {
              this.suc({ message: res.errmsg })
              this.getList();
              this.ruleForm = { facilityTypeName: '', id: '', parentFacilityTypeUuid: '', remark: '', facilityTypeCode: '', };
              this.editShow = false;
            } else {
              this.err({ message: res.errmsg })
            }
          })
          this.addVisible = false;
        } else {
          return false;
        }
      });
    },
    // 删除
    delRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.sendGet('/facilityType/delete', { id: row.id }).then(res => {
          if (res.errcode == 0) {
            this.suc({ message: res.errmsg })
            this.getList();
          } else {
            this.err({ message: res.errmsg })
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' });
      });
    },
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {}
};
</script>

<style scoped lang="scss"></style>
